<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="eidt-el-form">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="ruleForm.name" :disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="ruleForm.phone" :disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="证件总数量" prop="total">
              <el-input v-model="ruleForm.total" min='0' type="number"></el-input>
            </el-form-item>
            <el-form-item label="证件类型" prop="documentTypeId">
              <el-select v-model="ruleForm.documentTypeId" placeholder="选择证件类型" :disabled="disabled">
                <el-option
                  v-for="(type,index) in DocumentType"
                  :label="type.name"
                  :key="index"
                  :value="type.documentTypeId">
                  <div @click="getChangeType(type)">{{type.name}}</div>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="已使用数量" prop="useNumber" v-show="distributeId!=''">
              <el-input v-model="ruleForm.useNumber" min='0' type="number" :disabled="disabled"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>
import checkPhone from "../../common/checkPhone";

export default {
  data() {
    return {
      rules: {
        name: [{ required: true, message: "请填写姓名", trigger: "blur" }],
        phone: [{ required: true, validator: checkPhone, trigger: "blur" }],
        distributeId: [{ required: true, message: "证件类型不能为空", trigger: "blur" }],
        total: [{ required: true, message: "数量不能为空", trigger: "blur" }]
      },
      ruleForm: {
        distributeId:"",
        name: "",
        phone: "",
        total:"",
        typeName:"",
        projectId:localStorage.getItem('project_Id'),
        useNumber:0
      },
      editImg:"",
      distributeId: "",
      dataList:[],
      DocumentType:[],
      disabled:false
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    arrarList: {
      type: Array,
      default() {
        return [];
      }
    },
    editData: {
      type: Object
    }
  },
  created(){ 
    this.getDocumentType();
  },
  watch: {
    editData: function() {
      const item = this.editData;
      this.ruleForm = item;
      this.distributeId = item.distributeId
      this.disabled = true
      // this.editImg = item.avatar;
    }
  },
  methods: {
    saveClick(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {   
          const loading =this.$loading({
              lock: true,
              text: 'Loading',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            });
          if (this.distributeId == '' || this.distributeId == undefined) {
            this.$axios.post("/Distribute/", this.ruleForm).then(res => {
                loading.close();
              if (res.data.hasError == false) {
                this.$message.success("保存成功！");
                this.goBack();
              } else {
                this.$message.error(res.data.message);
              }
            });
          } else {
            this.$axios.patch("/Distribute/", this.ruleForm)
              .then(res => {
                loading.close();
                if (res.data.hasError == false) {
                  this.$message.success("编辑成功！");
                  this.goBack();
                } else {
                  this.$message.error(res.data.message);
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$emit("backFunStu");
      this.ticketCom_Id = '';
      this.disabled = false;
      this.ruleForm.useNumber=0;
      this.$refs['ruleForm'].resetFields();
    },
    /*getUploadImg(imgSrc){
      this.ruleForm.banner = imgSrc;
    },*/
    getDocumentType(){
      this.$axios.get('/DocumentType/Project/'+this.ruleForm.projectId).then(res =>{
        if (res.data.hasError == false) {
          this.DocumentType = [...res.data.data]
        }
      })
    },
    getChangeType(type){
      this.ruleForm.documentTypeId = type.documentTypeId;
      this.ruleForm.typeName = type.name;
    }
  }
};
</script>